﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.Geodatabase;
using NcgmpDataAccess.Utilities;

namespace NcgmpDataAccess
{
    public abstract class NcgmpAccess 
    {
        public abstract bool IsFeatureClass
        {
            get;
        }

        public abstract string TableName
        {
            get;
        }

        private ESRI.ArcGIS.Geodatabase.IWorkspace m_theWorkspace;

        public ESRI.ArcGIS.Geodatabase.IWorkspace TheWorkspace
        {
            get { return m_theWorkspace; }
            internal set { m_theWorkspace = value; }
        }

        private Dictionary<string, NcgmpFeature> m_featureCollection = new Dictionary<string, NcgmpFeature>();

        public Dictionary<string, NcgmpFeature> FeatureCollection
        {
            get { return m_featureCollection; }            
        }

        public abstract NcgmpFeature CreateFeature(Dictionary<string, object> attributeNameValuePairs);

        public abstract bool UpdateFeatures(NcgmpFeature theFeature);

        public bool RetrieveFeatures(string sqlWhereClause)
        {
            IQueryFilter QF = new QueryFilterClass();
            QF.WhereClause = sqlWhereClause;

            try
            {
                switch (IsFeatureClass)
                {
                    case true:
                        IFeatureClass theFeatureClass = commonFunctions.OpenFeatureClass(m_theWorkspace, TableName);
                        IFeatureCursor theFeatureCursor = theFeatureClass.Search(QF, false);
                        IFeature aFeature = theFeatureCursor.NextFeature();
                        while (aFeature != null)
                        {
                            NcgmpFeature theFeature = ConstructTypedFeature(aFeature);
                            m_featureCollection.Add(theFeature.Identifier, theFeature);
                            aFeature = theFeatureCursor.NextFeature();
                        }
                        break;

                    case false:
                        ITable theTable = commonFunctions.OpenTable(m_theWorkspace, TableName);
                        ICursor theCursor = theTable.Search(QF, false);
                        IRow aRow = theCursor.NextRow();
                        while (aRow != null)
                        {
                            NcgmpFeature theFeature = ConstructTypedRow(aRow);
                            m_featureCollection.Add(theFeature.Identifier, theFeature);
                            aRow = theCursor.NextRow();
                        }
                        break;
                }

                if (m_featureCollection.Count > 0) { return true; }
                else { return false; }
            }
            catch (Exception err) { throw err; }
        }

        public bool DeleteFeatures(NcgmpFeature theFeature)
        {
            IQueryFilter QF = new QueryFilterClass();
            QF.WhereClause = TableName + "_ID = '" + theFeature.Identifier + "'";

            ITable theTable = commonFunctions.OpenTable(m_theWorkspace, TableName);
            if (theTable.RowCount(QF) == 0) { return false; }
            
            //Edit session!
            theTable.DeleteSearchedRows(QF);

            return true;
        }

        public abstract bool SaveFeatures();

        public abstract NcgmpFeature ConstructTypedFeature(IFeature ArcObjectsFeature);

        public abstract NcgmpFeature ConstructTypedRow(IRow ArcObjectsRow);

    }
}
